import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:seedfour/app/theme.dart';

import '../../../constants.dart';
import 'logic.dart';

class SealDetailPage extends StatelessWidget {
  SealDetailPage({Key? key}) : super(key: key);

  final logic = Get.find<SealDetailLogic>();
  final state = Get.find<SealDetailLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('用印详情'),
        leading: IconButton(
          icon: Icon(
            Icons.arrow_back_ios,
            size: 16.w,
          ),
          onPressed: () {
            Get.back();
          },
        ),
      ),
      body: Obx(() {
        if (state.isLoading.value) {
          return const Center(child: CircularProgressIndicator());
        } else {
          return ListView(children: [
            SizedBox(height: kSpaceMedium),
            _buildBaseInfo(),
            SizedBox(height: kSpaceMedium),
            _buildApplyInfo(),
          ]);
        }
      }),
    );
  }

  Widget _buildBaseInfo() {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: kSpaceSmall),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(kRadiusMedium),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: EdgeInsets.all(kSpaceMedium),
            child: Text(
              '基本信息',
              style: TextStyle(
                fontSize: kFontSizeMedium,
                color: AppColor.colorFontBlack,
                fontWeight: AppFont.fontMedium,
              ),
            ),
          ),
          const Divider(),
          SizedBox(height: kSpaceMedium),
          _buildItem(
              '审批状态',
              sealApplyStatus[state.sealDetail.value!.commonApply!.status!] ??
                  ''),
          SizedBox(height: kSpaceMedium),
          _buildItem(
              '申请类型',
              SealPaperType
                  .values[state.sealDetail.value!.commonApply!.type!].label),
          SizedBox(height: kSpaceMedium),
          if (state.sealDetail.value != null &&
              state.sealDetail.value!.caseInfo != null)
            _buildItem(
              '关联案件',
              state.sealDetail.value!.caseInfo == null
                  ? ''
                  : state.sealDetail.value!.caseInfo!.caseNameStr ?? '',
            ),
          SizedBox(height: kSpaceMedium),
        ],
      ),
    );
  }

  Widget _buildApplyInfo() {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: kSpaceSmall),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(kRadiusMedium),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: EdgeInsets.all(kSpaceMedium),
            child: Text(
              '申请内容',
              style: TextStyle(
                fontSize: kFontSizeMedium,
                color: AppColor.colorFontBlack,
                fontWeight: AppFont.fontMedium,
              ),
            ),
          ),
          const Divider(),
          SizedBox(height: kSpaceMedium),
          _buildItem(
              '申请人', state.sealDetail.value!.commonApply!.creatorName ?? ''),
          SizedBox(height: kSpaceMedium),
          _buildItem('申请印章',
              sealType[state.sealDetail.value!.commonApply!.sealType!] ?? ''),
          SizedBox(height: kSpaceMedium),
          _buildItem(
              '文件份数', state.sealDetail.value!.commonApply!.printNum.toString()),
          SizedBox(height: kSpaceMedium),
          _buildItem('申请备注', state.sealDetail.value!.commonApply!.remark ?? ''),
          SizedBox(height: kSpaceMedium),
          Padding(
            padding: EdgeInsets.symmetric(horizontal: kSpaceMedium),
            child: Text(
              '用印文件',
              style: TextStyle(
                fontSize: kFontSizeSmall,
                color: AppColor.colorFontBlack,
              ),
            ),
          ),
          SizedBox(height: kSpaceMedium),
          Padding(
            padding: EdgeInsets.symmetric(horizontal: kSpaceMedium),
            child: Column(children: [
              for (var item in state.sealDetail.value!.attachFiles ?? [])
                Container(
                  padding: EdgeInsets.only(left: kSpaceMedium),
                  margin: EdgeInsets.only(top: kSpaceMini),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(kRadiusMedium),
                    color: AppColor.colorTagBlue,
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Expanded(
                        child: Text(
                          item.name,
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                            fontSize: kFontSizeSmall,
                            color: AppColor.colorFontBlue,
                            fontWeight: AppFont.fontRegular,
                          ),
                        ),
                      ),
                      IconButton(
                        onPressed: () => logic.downloadFile(item),
                        icon: const Icon(Icons.file_download_outlined,
                            color: Color(0xFFC7C8DB)),
                      ),
                    ],
                  ),
                ),
            ]),
          ),
          SizedBox(height: kSpaceMedium),
        ],
      ),
    );
  }

  Widget _buildItem(String title, String value) {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: kSpaceMedium),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            title,
            style: TextStyle(
              fontSize: kFontSizeSmall,
              color: AppColor.colorFontBlack,
            ),
          ),
          SizedBox(width: kSpaceMedium),
          Expanded(
            child: Text(
              textAlign: TextAlign.right,
              value,
              maxLines: 3,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                fontSize: kFontSizeSmall,
                color: AppColor.colorFontBlack,
              ),
            ),
          ),
        ],
      ),
    );
  }
}
